require 'calabash-android/calabash_steps'
require 'calabash-android/operations'

wait_time_out = 10

When(/^I select a language "(.*?)"$/) do |language|
  languageItem = "TextView text:'#{language}'"
  wait_for(:timeout => wait_time_out) do 
    element_exists(languageItem)
  end
  touch(languageItem)
end

Transform /^(-?\d+)$/ do |number|
  number.to_i
end

When(/^I select (-?\d+) th on a list of items$/) do |index|
  item_dsl = "ListView TextView"
  item_query = query(item_dsl)
  wait_for(:timeout => wait_time_out) do 
    element_exists(item_dsl)
  end
  touch(item_query[index])
end


When /^I select "([^"]*)" on dialog$/ do |button|
  button_selector = "FrameLayout Button text:'#{button}'"
  wait_for(:timeout => wait_time_out) do 
    element_exists(button_selector)
  end  
  touch(button_selector)
end

When /^I going to see an dialog with "([^"]*)" and "([^"]*)" options$/ do |first_button,second_button|
  first_button_query = query("FrameLayout Button text:'#{first_button}'")
  second_button_query = query("FrameLayout Button text:'#{second_button}'")
  assert first_button_query.size == 1
  assert second_button_query.size == 1
end


When /^I generate a file that big enough to fill the folder "([^"]*)" called "([^"]*)"$/ do |folder_path,file_name|
  max_file_size = 1
  could_save_file = true
  begin
    max_file_size += 1
    File.open("#{file_name}_#{max_file_size}", 'wb') do|file|
      file.write(" " * 1024 * 1024)
    end
    begin
      push("#{file_name}_#{max_file_size}","#{folder_path}/#{file_name}_#{max_file_size}")
      could_save_file = true
    rescue Exception => e
      could_save_file = false
    end
  end while could_save_file
  @max_file_size = max_file_size -1
  @file_name = file_name
  @folder_path = folder_path
end

When /^I send the "([^"]*)" to the "([^"]*)" folder$/ do |file_name,folder_path|
  push(file_name,folder_path)
end

When /^I going to see an dialog that informs that is requires more space$/ do
  button_selector = "FrameLayout Button"
  wait_for(:timeout => wait_time_out) do 
    element_exists(button_selector)
  end
end

When /^I will clean the folder$/ do
  @max_file_size.times do |i|
    File.open("#{@file_name}_#{i+1}", 'wb') do|file|
      file.write(" ")
    end
    push("#{@file_name}_#{i+1}","#{@folder_path}/#{@file_name}_#{i+1}")
    File.delete("#{@file_name}_#{i+1}")
  end
  could_save_file = true
end

When /^I save the database from the app$/ do
  pull("/data/data/com.dferreira.languagesteach/databases/LanguagesTeach","LanguagesTeach")
end

When /^I restore the database from the app$/ do
  push("LanguagesTeach","/data/data/com.dferreira.languagesteach/databases/LanguagesTeach")
end

When(/^I check the checkbox number (-?\d+)$/) do |index|
  item_dsl = "Checkbox"
  item_query = query(item_dsl)
  unless query(item_dsl)[index-1]["checked"]
    performAction('toggle_numbered_checkbox', index)
  end
end

When(/^I uncheck the checkbox number (-?\d+)$/) do |index|
  item_dsl = "Checkbox"
  item_query = query(item_dsl)
  if query(item_dsl)[index-1]["checked"]
    performAction('toggle_numbered_checkbox', index)
  end
end

When(/^I set text from "([^"]*)" with  "([^"]*)"$/)  do |item_dsl,text|
  query(item_dsl, :setText => text)
end

